import { _decorator, Collider2D, Component, Node,Contact2DType,IPhysics2DContact } from 'cc';
import { PropItem } from './PropItem';
import { PropType } from '../Common';
import { TankShoot } from './TankShoot';
import { Healthy } from './Healthy';
import { TankAnim } from './TankAnim';
import { EventMgr } from '../../EventMgr/EventMgr';
import { GameMgr } from '../GameMgr';
const { ccclass, property } = _decorator;

//这个组件控制了道具
@ccclass('TankPlayer')
export class TankPlayer extends Component {
    private collider;
    private tShoot:TankShoot=null;
    private tankH:Healthy=null;
    private tankAnim:TankAnim=null;

    protected onLoad(): void {
        this.tankH=this.getComponent(Healthy);
        this.tankAnim=this.getComponent(TankAnim);
        this.tShoot=this.getComponent(TankShoot);
        
        this.collider=this.getComponent(Collider2D);

        this.collider.on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this);
        EventMgr.Inst.addEvent("GAME_START",this.gameStart2,this);
        this.node.on("DIE",this.die,this);
    }

    gameStart2(){
        //console.log("tankPlayer gameStart");
    }

    die(){
        GameMgr.Inst.createPlayer();
        //console.log("tankPlayer die");
    }

    protected start(): void {
        
        this.eatCapProp();
    }


    
    protected onDestroy(): void {
        this.collider.off(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this);
        EventMgr.Inst.removeEvent("GAME_START",this.gameStart2,this);
        this.node.off("DIE",this.die,this);

    }

    eatDamageProp(){

                        this.tShoot.eatDamageProp();
    }

    //这个是无敌
    eatCapProp(){
        console.log("eat capProp");
        this.tankH.setInvincible(true);
        this.tankAnim.toggleRing(true);
        
        this.scheduleOnce(()=>{
            this.tankH.setInvincible(false);
            this.tankAnim.toggleRing(false);
        },GameMgr.Inst.getQuickTime(10,1));
    }

    onBeginContact(self:Collider2D,other:Collider2D,  contact: IPhysics2DContact | null){

        //console.log("contact"+contact);
        //这里没有做碰撞过滤想着太麻烦了
        if(other ){

            let pi:PropItem =other.getComponent(PropItem);
            if(pi){
            console.log("player begincontact")
                let type =pi.pType;
                switch(type){
                    case PropType.addDamage:
                        pi.desSelf();
                        this.eatDamageProp();
                        break;
                    case PropType.cap:
                        pi.desSelf();
                        this.eatCapProp();
                        break;
                    case PropType.addTank:
                        pi.desSelf();
                        GameMgr.Inst.addPlayerLifeCount();
                        break;
                    case PropType.timeStop:
                            pi.desSelf();
                            GameMgr.Inst.pauseAI();
                            break;
                    case PropType.bomb:
                                pi.desSelf();
                                GameMgr.Inst.destoryAllEnemy();
                                break;
                    case PropType.saveCamp:
                                    pi.desSelf();
                                EventMgr.Inst.sendEvent("SAVE_CAMP");
                                    break;
                }
            }
        }
    }
}


